package com.android.juliomedina.offline;

import java.util.ArrayList;

import com.android.juliomedina.R;
import com.android.juliomedina.DB.Reviews_DBHelperFeed;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;

/*
 * @author globussoft
 * 
 */
public class reviews_offline extends ListActivity {

	private ArrayList<String> results = new ArrayList<String>();
	private String tableName = Reviews_DBHelperFeed.DATABASE_TABLE_FEED;
	private SQLiteDatabase newDB;
	private final String MY_DATABASE_NAME = "revfeed.db";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		openAndQueryDatabase();
		displayResultList();
	}

	private void displayResultList() {
		TextView tView = new TextView(this);
		tView.setText("Reviews :");
		getListView().addHeaderView(tView);
		setListAdapter(new ArrayAdapter<String>(this,
				R.layout.simple_list_item_2, results));
		getListView().setTextFilterEnabled(true);
	}

	private void openAndQueryDatabase() {
		try {
			newDB = this.openOrCreateDatabase(MY_DATABASE_NAME,
					SQLiteDatabase.OPEN_READWRITE, null);
			Cursor c = newDB.query(tableName, null, "_id>0", null, null, null,
					null);
			if (c != null) {
				if (c.moveToFirst()) {
					do {
						String event_name = c.getString(c
								.getColumnIndex("title"));
						String event_date = c.getString(c
								.getColumnIndex("description"));
						String event_loc = c
								.getString(c.getColumnIndex("date"));
						results.add("Title: " + event_name
								+ "\n\nDescription: " + event_loc
								+ "\n\nDate: " + event_date);
					} while (c.moveToNext());
				}
			}
		} catch (SQLiteException se) {
			Log.e(getClass().getSimpleName(),
					"Could not create or Open the database");
		} finally {
			if (newDB != null)
				newDB.close();
		}

	}

}
